package com.inei.cie.dao;

import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.inei.cie.entidades.P8;

public class CapituloVIIIDao {

	private UtilDB util;
	SQLiteDatabase db;
	Cursor cursor = null;

	public CapituloVIIIDao(Context context) {
		util = new UtilDB(context);
	}

	public void crearOtrasEdificacionesLocal(String id_local, int Nro_Pred,
			int nroP, int nroL, int nroC, int nroM) {
		for (int i = 0; i < nroP; i++) {
			if (!existePatio(id_local, Nro_Pred, (i + 1))) {
				db = util.getWritableDatabase();
				ContentValues c = new ContentValues();
				c.put("id_local", id_local);
				c.put("P8_2", "P - " + (i + 1));
				c.put("Nro_Pred", Nro_Pred);
				db.insert("P8", null, c);
				db.close();
			}
		}
		for (int i = 0; i < nroL; i++) {
			if (!existeLosa(id_local, Nro_Pred, (i + 1))) {
				db = util.getWritableDatabase();
				ContentValues c = new ContentValues();
				c.put("id_local", id_local);
				c.put("P8_2", "LD - " + (i + 1));
				c.put("Nro_Pred", Nro_Pred);
				db.insert("P8", null, c);
				db.close();
			}
		}
		for (int i = 0; i < nroC; i++) {
			if (!existeCisterna(id_local, Nro_Pred, (i + 1))) {
				db = util.getWritableDatabase();
				ContentValues c = new ContentValues();
				c.put("id_local", id_local);
				c.put("P8_2", "CTE - " + (i + 1));
				c.put("Nro_Pred", Nro_Pred);
				db.insert("P8", null, c);
				db.close();
			}
		}
		for (int i = 0; i < nroM; i++) {
			if (!existeMuro(id_local, Nro_Pred, (i + 1))) {
				db = util.getWritableDatabase();
				ContentValues c = new ContentValues();
				c.put("id_local", id_local);
				c.put("P8_2", "MC - " + (i + 1));
				c.put("Nro_Pred", Nro_Pred);
				db.insert("P8", null, c);
				db.close();
			}
		}
		Cerrar();
	}

	public boolean existePatio(String id_local, int Nro_Pred, int patio) {
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"select * from P8 where id_local=? and P8_2=? and Nro_Pred=?",
				new String[] { id_local + "", "P - " + patio + "",
						Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			Cerrar();
			return true;
		} else {
			Cerrar();
			return false;
		}
	}

	public boolean existeLosa(String id_local, int Nro_Pred, int losa) {
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"select * from P8 where id_local=? and P8_2=? and Nro_Pred=?",
				new String[] { id_local + "", "LD - " + losa + "",
						Nro_Pred + "" });

		if (cursor.moveToFirst()) {
			Cerrar();
			return true;
		} else {
			Cerrar();
			return false;
		}
	}

	public boolean existeCisterna(String id_local, int Nro_Pred, int cisterna) {
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"select * from P8 where id_local=? and P8_2=? and Nro_Pred=?",
				new String[] { id_local + "", "CTE - " + cisterna + "",
						Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			Cerrar();
			return true;
		} else {
			Cerrar();
			return false;
		}
	}

	public boolean existeMuro(String id_local, int Nro_Pred, int muro) {
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"select * from P8 where id_local=? and P8_2=? and Nro_Pred=?",
				new String[] { id_local + "", "MC - " + muro + "",
						Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			Cerrar();
			return true;
		} else {
			Cerrar();
			return false;
		}
	}

	public boolean ExisteOtrosLocal(String id_local, int Nro_Pred) {
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"select * from P8 where id_local=? and Nro_Pred=?",
				new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			Cerrar();
			return true;
		} else {
			Cerrar();
			return false;
		}
	}

	public int totalPatios(String id_local, int Nro_Pred) {
		int nroPatios = 0;
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"select * from P5 where id_local=? and Nro_Pred=?",
				new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				nroPatios += cursor.getInt(cursor.getColumnIndex("P5_Tot_P"));
				cursor.moveToNext();
			}
			Cerrar();
			return nroPatios;
		} else {
			Cerrar();
			return nroPatios;
		}
	}

	public int totalLosasDeportivas(String id_local, int Nro_Pred) {
		int nroLosasDeportivas = 0;
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"select * from P5 where id_local=? and Nro_Pred=?",
				new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				nroLosasDeportivas += cursor.getInt(cursor
						.getColumnIndex("P5_Tot_LD"));
				cursor.moveToNext();
			}
			Cerrar();
			return nroLosasDeportivas;
		} else {
			Cerrar();
			return nroLosasDeportivas;
		}
	}

	public int totalCisternas(String id_local, int Nro_Pred) {
		int nroCisternas = 0;
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"select * from P5 where id_local=? and Nro_Pred=?",
				new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				nroCisternas += cursor.getInt(cursor
						.getColumnIndex("P5_Tot_CTE"));
				cursor.moveToNext();
			}
			Cerrar();
			return nroCisternas;
		} else {
			Cerrar();
			return nroCisternas;
		}
	}

	public int totalMuros(String id_local, int Nro_Pred) {
		int nroMuros = 0;
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"select * from P5 where id_local=? and Nro_Pred=?",
				new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				nroMuros += cursor.getInt(cursor.getColumnIndex("P5_Tot_MC"));
				cursor.moveToNext();
			}
			Cerrar();
			return nroMuros;
		} else {
			Cerrar();
			return nroMuros;
		}
	}

	public List<P8> obtenerListadoOtros(String id_local, int Nro_Pred,
			int TipoOtro) {
		List<P8> valores = new ArrayList<P8>();
		db = util.getReadableDatabase();
		String sql = "";
		if (TipoOtro == 1) {
			sql = "select * from P8 where id_local=? and Nro_Pred=? and P8_2 like 'P - %'";
		} else if (TipoOtro == 2) {
			sql = "select * from P8 where id_local=? and Nro_Pred=? and P8_2 like 'LD - %'";
		} else if (TipoOtro == 3) {
			sql = "select * from P8 where id_local=? and Nro_Pred=? and P8_2 like 'CTE - %'";
		} else if (TipoOtro == 4) {
			sql = "select * from P8 where id_local=? and Nro_Pred=? and P8_2 like 'MC - %'";
		}
		cursor = db
				.rawQuery(sql, new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				P8 obj = new P8();
				obj.setId_local(cursor.getString(cursor
						.getColumnIndex("id_local")));
				obj.setNro_Pred(cursor.getInt(cursor.getColumnIndex("Nro_Pred")));
				obj.setP8_2(cursor.getString(cursor.getColumnIndex("P8_2")));
				boolean estado = cursor.getInt(cursor
						.getColumnIndex("SwCompleto")) == 1 ? true : false;
				obj.setSwCompleto(estado);
				boolean enviado = cursor.getInt(cursor
						.getColumnIndex("SwEnviado")) == 1 ? true : false;
				obj.setSwEnviado(enviado);
				valores.add(obj);
				cursor.moveToNext();
			}
		}
		Cerrar();
		return valores;
	}

	public P8 obtenerP8(String id_local, int Nro_Pred, int cod, int tipo) {
		P8 obj = null;
		db = util.getReadableDatabase();
		String sql = "";
		if (tipo == 1) {
			sql = "select * from P8 where id_local=? and Nro_Pred=? and P8_2 like 'P - "
					+ cod + "'";
		} else if (tipo == 2) {
			sql = "select * from P8 where id_local=? and Nro_Pred=? and P8_2 like 'LD - "
					+ cod + "'";
		} else if (tipo == 3) {
			sql = "select * from P8 where id_local=? and Nro_Pred=? and P8_2 like 'CTE - "
					+ cod + "'";
		} else if (tipo == 4) {
			sql = "select * from P8 where id_local=? and Nro_Pred=? and P8_2 like 'MC - "
					+ cod + "'";
		}
		cursor = db
				.rawQuery(sql, new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				obj = new P8();
				obj.setId_local(cursor.getString(cursor
						.getColumnIndex("id_local")));
				obj.setP8_2(cursor.getString(cursor.getColumnIndex("P8_2")));
				obj.setP8_area_Tip(cursor.getInt(cursor
						.getColumnIndex("P8_area_Tip")));
				obj.setP8_area(cursor.getFloat(cursor.getColumnIndex("P8_area")));
				obj.setNro_Pred(cursor.getInt(cursor.getColumnIndex("Nro_Pred")));
				obj.setP8_ejecuto(cursor.getInt(cursor
						.getColumnIndex("P8_ejecuto")));
				obj.setP8_Est_E(cursor.getInt(cursor.getColumnIndex("P8_Est_E")));
				obj.setP8_Ant(cursor.getInt(cursor.getColumnIndex("P8_Ant")));
				obj.setP8_RecTec(cursor.getInt(cursor
						.getColumnIndex("P8_RecTec")));
				obj.setP8_Est_PaLo(cursor.getInt(cursor
						.getColumnIndex("P8_Est_PaLo")));
				obj.setP8_longitud(cursor.getString(cursor
						.getColumnIndex("P8_longitud")));
				obj.setP8_altura(cursor.getString(cursor
						.getColumnIndex("P8_altura")));
				obj.setP8_ejecuto_O(cursor.getString(cursor
						.getColumnIndex("P8_ejecuto_O")));
				obj.setP8_Obs(cursor.getString(cursor.getColumnIndex("P8_Obs")));
				cursor.moveToNext();
			}
		}
		Cerrar();
		return obj;
	}

	public P8 obtenerP8(String id_local, int Nro_Pred, String cod) {
		P8 obj = null;
		db = util.getReadableDatabase();
		String sql = "";
		sql = "select * from P8 where id_local=? and Nro_Pred=? and P8_2=?";
		cursor = db.rawQuery(sql, new String[] { id_local + "", Nro_Pred + "",
				cod + "" });
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				obj = new P8();
				obj.setId_local(cursor.getString(cursor
						.getColumnIndex("id_local")));
				obj.setP8_2(cursor.getString(cursor.getColumnIndex("P8_2")));
				obj.setP8_area_Tip(cursor.getInt(cursor
						.getColumnIndex("P8_area_Tip")));
				obj.setP8_area(cursor.getFloat(cursor.getColumnIndex("P8_area")));
				obj.setNro_Pred(cursor.getInt(cursor.getColumnIndex("Nro_Pred")));
				obj.setP8_ejecuto(cursor.getInt(cursor
						.getColumnIndex("P8_ejecuto")));
				obj.setP8_Est_E(cursor.getInt(cursor.getColumnIndex("P8_Est_E")));
				obj.setP8_Ant(cursor.getInt(cursor.getColumnIndex("P8_Ant")));
				obj.setP8_RecTec(cursor.getInt(cursor
						.getColumnIndex("P8_RecTec")));
				obj.setP8_Est_PaLo(cursor.getInt(cursor
						.getColumnIndex("P8_Est_PaLo")));
				obj.setP8_longitud(cursor.getString(cursor
						.getColumnIndex("P8_longitud")));
				obj.setP8_altura(cursor.getString(cursor
						.getColumnIndex("P8_altura")));
				obj.setP8_Obs(cursor.getString(cursor.getColumnIndex("P8_Obs")));
				obj.setP8_ejecuto_O(cursor.getString(cursor
						.getColumnIndex("P8_ejecuto_O")));
				boolean estado = cursor.getInt(cursor
						.getColumnIndex("SwCompleto")) == 1 ? true : false;
				obj.setSwCompleto(estado);
				boolean enviado = cursor.getInt(cursor
						.getColumnIndex("SwEnviado")) == 1 ? true : false;
				obj.setSwEnviado(enviado);
				cursor.moveToNext();
			}
		}
		Cerrar();
		return obj;
	}

	public void actualizarP8(P8 obj, int tipo) {
		db = util.getWritableDatabase();
		ContentValues valores = new ContentValues();
		valores.put("P8_area_Tip", obj.getP8_area_Tip());
		valores.put("P8_area", obj.getP8_area());
		valores.put("P8_predio", obj.getNro_Pred());
		valores.put("P8_ejecuto", obj.getP8_ejecuto());
		valores.put("P8_Est_E", obj.getP8_Est_E());
		valores.put("P8_Ant", obj.getP8_Ant());
		valores.put("P8_RecTec", obj.getP8_RecTec());
		valores.put("P8_Est_PaLo", obj.getP8_Est_PaLo());
		valores.put("P8_longitud", obj.getP8_longitud());
		valores.put("P8_altura", obj.getP8_altura());
		valores.put("P8_ejecuto_O", obj.getP8_ejecuto_O());
		valores.put("P8_Obs", obj.getP8_Obs());
		valores.put("SwCompleto", obj.isSwCompleto());

		if (tipo == 1) {
			int ind = db.update("P8", valores,
					"id_local=? and P8_2=? and Nro_Pred=?",
					new String[] { obj.getId_local() + "", obj.getP8_2() + "",
							obj.getNro_Pred() + "" });
		} else if (tipo == 2) {
			int ind = db.update("P8", valores,
					"id_local=? and P8_2=? and Nro_Pred=?",
					new String[] { obj.getId_local() + "", obj.getP8_2() + "",
							obj.getNro_Pred() + "" });
		} else if (tipo == 3) {
			int ind = db.update("P8", valores,
					"id_local=? and P8_2=? and Nro_Pred=?",
					new String[] { obj.getId_local() + "", obj.getP8_2() + "",
							obj.getNro_Pred() + "" });
		} else if (tipo == 4) {
			int ind = db.update("P8", valores,
					"id_local=? and P8_2=? and Nro_Pred=?",
					new String[] { obj.getId_local() + "", obj.getP8_2() + "",
							obj.getNro_Pred() + "" });
		}
		Cerrar();
	}

	public void eliminarPatio(String id_local, int Nro_Pred, int pos) {
		int total = 0;
		db = util.getWritableDatabase();
		db.delete("P8", "id_local=? and Nro_Pred=? and P8_2=?", new String[] {
				id_local + "", Nro_Pred + "", "P - " + pos });
		db.close();
		List<P8> patios = obtenerListadoOtros(id_local, Nro_Pred, 1);

		if (patios != null) {
			for (int i = pos; i <= patios.size(); i++) {
				db = util.getWritableDatabase();
				ContentValues c = new ContentValues();
				c.put("P8_2", "P - " + (i));
				db.update("P8", c, "id_local=? and Nro_Pred=? and P8_2=?",
						new String[] { id_local + "", Nro_Pred + "",
								"P - " + (i + 1) });
				db.close();
			}
		}

		db = util.getReadableDatabase();
		cursor = db
				.rawQuery(
						"select * from P8 where id_local=? and Nro_Pred=? and P8_2 like 'P - %'",
						new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			total = cursor.getCount();
		}
		db.close();
		db = util.getWritableDatabase();
		ContentValues c = new ContentValues();
		c.put("P5_Tot_P", total);
		db.update("P5", c, "id_local=? and Nro_Pred=?", new String[] {
				id_local + "", Nro_Pred + "" });
		Cerrar();
	}

	public void eliminarLosa(String id_local, int Nro_Pred, int pos) {
		int total = 0;
		db = util.getWritableDatabase();
		db.delete("P8", "id_local=? and Nro_Pred=? and P8_2=?", new String[] {
				id_local + "", Nro_Pred + "", "LD - " + pos });
		db.close();
		List<P8> losas = obtenerListadoOtros(id_local, Nro_Pred, 2);

		if (losas != null) {
			for (int i = pos; i <= losas.size(); i++) {
				db = util.getWritableDatabase();
				ContentValues c = new ContentValues();
				c.put("P8_2", "LD - " + (i));
				db.update("P8", c, "id_local=? and Nro_Pred=? and P8_2=?",
						new String[] { id_local + "", Nro_Pred + "",
								"LD - " + (i + 1) });
				db.close();
			}
		}

		db = util.getReadableDatabase();
		cursor = db
				.rawQuery(
						"select * from P8 where id_local=? and Nro_Pred=? and P8_2 like 'LD - %'",
						new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			total = cursor.getCount();
		}
		db.close();
		db = util.getWritableDatabase();
		ContentValues c = new ContentValues();
		c.put("P5_Tot_LD", total);
		db.update("P5", c, "id_local=? and Nro_Pred=?", new String[] {
				id_local + "", Nro_Pred + "" });
		Cerrar();
	}

	public void eliminarCisterna(String id_local, int Nro_Pred, int pos) {
		int total = 0;
		db = util.getWritableDatabase();
		db.delete("P8", "id_local=? and Nro_Pred=? and P8_2=?", new String[] {
				id_local + "", Nro_Pred + "", "CTE - " + pos });
		db.close();
		List<P8> cisternas = obtenerListadoOtros(id_local, Nro_Pred, 3);

		if (cisternas != null) {
			for (int i = pos; i <= cisternas.size(); i++) {
				db = util.getWritableDatabase();
				ContentValues c = new ContentValues();
				c.put("P8_2", "CTE - " + (i));
				db.update("P8", c, "id_local=? and Nro_Pred=? and P8_2=?",
						new String[] { id_local + "", Nro_Pred + "",
								"CTE - " + (i + 1) });
				db.close();
			}
		}

		db = util.getReadableDatabase();
		cursor = db
				.rawQuery(
						"select * from P8 where id_local=? and Nro_Pred=? and P8_2 like 'CTE - %'",
						new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			total = cursor.getCount();
		}
		db.close();
		db = util.getWritableDatabase();
		ContentValues c = new ContentValues();
		c.put("P5_Tot_CTE", total);
		db.update("P5", c, "id_local=? and Nro_Pred=?", new String[] {
				id_local + "", Nro_Pred + "" });
		Cerrar();
	}

	public void eliminarMuro(String id_local, int Nro_Pred, int pos) {
		int total = 0;
		db = util.getWritableDatabase();
		db.delete("P8", "id_local=? and Nro_Pred=? and P8_2=?", new String[] {
				id_local + "", Nro_Pred + "", "MC - " + pos });
		db.close();
		List<P8> muros = obtenerListadoOtros(id_local, Nro_Pred, 4);

		if (muros != null) {
			for (int i = pos; i <= muros.size(); i++) {
				db = util.getWritableDatabase();
				ContentValues c = new ContentValues();
				c.put("P8_2", "MC - " + (i));
				db.update("P8", c, "id_local=? and Nro_Pred=? and P8_2=?",
						new String[] { id_local + "", Nro_Pred + "",
								"MC - " + (i + 1) });
				db.close();
			}
		}

		db = util.getReadableDatabase();
		cursor = db
				.rawQuery(
						"select * from P8 where id_local=? and Nro_Pred=? and P8_2 like 'MC - %'",
						new String[] { id_local + "", Nro_Pred + "" });
		if (cursor.moveToFirst()) {
			total = cursor.getCount();
		}
		db.close();
		db = util.getWritableDatabase();
		ContentValues c = new ContentValues();
		c.put("P5_Tot_MC", total);
		db.update("P5", c, "id_local=? and Nro_Pred=?", new String[] {
				id_local + "", Nro_Pred + "" });
		Cerrar();
	}

	protected void Cerrar() {
		cursor.close();
		db.close();
	}
}
